home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Applications / NIH Image 1.55 / Macros / Plotting Macros < prev    next >
Encoding:
Text File  |  1994-02-17  |  8.5 KB  |  347 lines  |  [TEXT/Imag]

  1. gin,1,histogram[i]*scale);
  2.     SetForegroundColor(i);
  3.     fill;
  4.  end;
  5.   SelectAll;
  6.   FlipVertical;
  7.   KillRoi;
  8.   RestoreState;
  9. end;
  10.  
  11. macro 'Plot XY Coordinates';
  12. {Plots the X-Y Coordinates of the current ROI.}
  13. var
  14.   i,w,h,width,height:integer;
  15.   xbase,ybase,RoiWidth,RoiHeight:integer
  16.   x,y,scale,xmax,ymax:real 
  17. begin
  18.   RequiresVersion(1.48);
  19.   if nCoordinates=0 then begin
  20.     PutMessage('No XY-Coordinates currently available.');
  21.     exit;
  22.   end;
  23.   GetRoi(xbase,ybase,RoiWidth,RoiHeight);
  24.   SaveState;
  25.   InvertY(false);
  26.   xmax:=0;
  27.   ymax:=0;
  28.   for i:=1 to nCoordinates do begin
  29.     x:=xCoordinates[i];
  30.     y:=yCoordinates[i];
  31.     if x>xmax then xmax:=x;
  32.     if y>ymax then ymax:=y;
  33.   end;
  34.   scale:=sqrt((300*300)/(xmax*ymax));
  35.   if (xmax*scale)>500 then scale:=500/xmax;
  36.   if (ymax*scale)>500 then scale:=500/ymax;
  37.   SetForegroundColor(255);
  38.   SetBackgroundColor(0);
  39.   SetNewSize(xmax*scale+20,ymax*scale+20);
  40.   MakeNewWindow('Outline');
  41.   MoveTo(xCoordinates[1]*scale+10,yCoordinates[1]*scale+10);
  42.   for i:=2 to nCoordinates do
  43.     LineTo(xCoordinates[i]*scale+10,yCoordinates[i]*scale+10);
  44.   SetFont('Helvetica');
  45.   SetFontSize(12);
  46.   SetText('No background, Center');
  47.   GetPicSize(width,height);
  48.   MoveTo(width/2,height/3);
  49.   Writeln(nCoordinates:1,' coordinate pairs');
  50.   Writeln('Origin=',xbase:1,',',ybase:1);
  51.   RestoreState;
  52. end;
  53.  
  54.  
  55. procedure PlotProfile2(integrate:boolean);
  56. var
  57.   xmin,xmax,ymin,ymax,i,xscale,yscale:real;
  58.   width,height,margin,pwidth,pheight:integer;
  59.   count:integer;
  60.   ppv:integer; {Pixels per Value}
  61. begin
  62.   SaveState;
  63.   margin:=40;
  64.   width:=500;
  65.   height:=300;
  66.   GetPlotData(count,ppv,ymin,ymax);
  67.   if count=0 then begin
  68.     PutMessage('No plot data available.');
  69.     exit;
  70.   end;
  71.   if integrate then begin
  72.      ymin:=ymin*ppv;
  73.      ymax:=ymax*ppv;
  74.   end;
  75.   xmin:=0;
  76.   xmax:=count-1;
  77.   SetNewSize(width,height);
  78.   SetForeground(255);
  79.   SetBackground(0);
  80.   MakeNewWindow('Plot');
  81.   pwidth:=width-2*margin;
  82.   pheight:=height-2*margin;
  83.   xscale:=pwidth/(xmax-xmin);
  84.   yscale:=pheight/(ymax-ymin);
  85.   SetForeground(255);
  86.   SetBackground(0); 
  87.   SetLineWidth(1); 
  88.   MoveTo(margin,margin);
  89.   if integrate then for i:=0 to count-1 do
  90.        LineTo(margin+i*xscale,margin+(PlotData[i]*ppv-ymin)*yscale)
  91.   else  for i:=0 to count-1 do
  92.         LineTo(margin+i*xscale,margin+(PlotData[i]-ymin)*yscale);
  93.   MakeRoi(margin,margin,pwidth+1,pheight+2);
  94.   MoveTo(margin,margin);
  95.   LineTo(margin+pwidth,margin);
  96.   MoveTo(margin,margin);
  97.   LineTo(margin,margin+pheight);
  98.   FlipVertical;
  99.   KillRoi;
  100.   SetFont('Geneva');
  101.   SetFontSize(9);
  102.   SetText('Centered');
  103.   MoveTo(margin+4,margin+pheight+12);
  104.   writeln(xmin:1:2);
  105.   MoveTo(margin+pwidth,margin+pheight+12);
  106.   writeln(xmax:1:2);
  107.   SetText('Right Justified');
  108.   MoveTo(margin-2,margin+pheight-5);
  109.   writeln(ymin:1:2);
  110.   MoveTo(margin-2,margin);
  111.   writeln(ymax:1:2);
  112.   RestoreState;
  113. end;
  114.  
  115.  
  116. macro 'Plot Profile';
  117. begin
  118.   PlotProfile2(false);
  119. end;
  120.  
  121. macro 'Plot Integrated Profile';
  122. begin
  123.   PlotProfile2(true);
  124. end;
  125.  
  126. macro 'Plot Radial Density Profiles [R]';
  127. var
  128.   x1,y1,x2,y2,pi,angle,delta:real;
  129.   LineWidth,i,nLines,radius,PlotWidth,PlotHeight:integer;
  130.   MinPlotWidth,hMargin,vMargin,PlotLeft,PlotTop:integer;
  131.   LeftMargin,RightMargin,TopMargin,BottomMargin:integer;
  132.   ImageWindow,PlotWindow:integer;
  133.   nPixels,mean,mode,min,max:real;
  134. begin
  135.   RequiresVersion(1.54);
  136.   SaveState;
  137.   GetLine(x1,y1,x2,y2,LineWidth);
  138.   if x1<0 then begin
  139.     PutMessage('Please select a point by clicking with the line tool.');
  140.     exit;
  141.   end;
  142.   radius:=GetNumber('Radius:',20);
  143.   nLines:=GetNumber('Number of Lines:',8);
  144.   MinPlotWidth:=140;
  145.   pi:=3.14159;
  146.   delta:=2.0*pi/nLines;
  147.   angle:=0.0;
  148.   PlotWidth:=radius;
  149.   if PlotWidth<MinPlotWidth then PlotWidth:=MinPlotWidth;
  150.   PlotHeight:=0.4*PlotWidth;
  151.   SetPlotSize(PlotWidth,PlotHeight);
  152.   MakeOvalRoi(x1-radius,y1-radius,radius*2,radius*2);
  153.   Measure;
  154.   GetResults(nPixels,mean,mode,min,max);
  155.   min:=min-10;
  156.   if min<0 then min:=0;
  157.   max:=max+10;
  158.   if max>255 then max:=255;
  159.   SetPlotScale(cValue(min),cValue(max));
  160.   SetPlotLabels(false);
  161.   hMargin:=5;
  162.   vMargin:=5;
  163.   LeftMargin:=38;
  164.   TopMargin:=10;
  165.   RightMargin:=20;
  166.   BottomMargin:=20;
  167.   PlotLeft:=hMargin-LeftMargin;
  168.   PlotTop:=vMargin-TopMargin;
  169.   SetNewSize(PlotWidth+2*hMargin,PlotHeight*nLines);
  170.   SetForegroundColor(255);
  171.   SetBackgroundColor(0);
  172.   ImageWindow:=PicNumber;
  173.   MakeNewWindow('Plots');
  174.   PlotWindow:=PicNumber;
  175.   SelectPic(ImageWindow);
  176.   for i:=1 TO nLines do begin
  177.     x2:=x1+round(radius*cos(angle));
  178.     y2:=y1+round(radius*sin(angle));
  179.     MakeLineRoi(x1,y1,x2,y2);
  180.     PlotProfile;
  181.     Copy;
  182.     SelectPic(PlotWindow);
  183.     MakeRoi(PlotLeft,PlotTop,PlotWidth+LeftMargin+RightMargin,
  184.           PlotHeight+TopMargin+BottomMargin);
  185.     Paste;
  186.     DoOr;
  187.     PlotTop:=PlotTop+PlotHeight-1;
  188.     SelectPic(ImageWindow);
  189.     angle:=angle+delta;
  190.   end;
  191.   RestoreState;
  192. end;
  193.  
  194.  
  195. macro 'Circular Profile Plot [C]';
  196. var
  197.   radius,pi,angle,dx,dy,delta:real;
  198.   x1,y1,x2,y2:real;
  199.   npoints,i,value,LineWidth,x,y,px:integer;
  200. begin
  201.   GetLine(x1,y1,x2,y2,LineWidth)
  202.   if x1<0 then begin
  203.     PutMessage('Please select a point by clicking with the line tool.');
  204.     exit;
  205.   end;
  206.   x:=x1+(x2-x1)/2;
  207.   y:=y1+(y2-y1)/2;
  208.   radius:=sqrt(sqr(x2-x1)+sqr(y2-y1))/2;
  209.   if radius<3 then begin
  210.     PutMessage('The line selection must be longer than 5 pixels.');
  211.     exit;
  212.   end;
  213.   npoints:=radius*2;
  214.   pi:=3.14159;
  215.   delta:=2.0*pi/npoints;
  216.   angle:=0.0;
  217.   px:=0;
  218.   for i:=1 TO npoints do begin
  219.     dx:=round(radius*cos(angle));
  220.     dy:=round(radius*sin(angle));
  221.     value:=GetPixel(x+dx,y+dy);
  222.     PutPixel(x+dx,y+dy,255);
  223.     PutPixel(px,0,value);
  224.     px:=px+1;
  225.     angle:=angle+delta;
  226.   end;
  227.   MakeLineRoi(0,0,npoints,0);
  228.   PlotProfile;
  229.   KillRoi;
  230. end;
  231.  
  232. macro 'Export Profile Plots…';
  233. var
  234.   y,yInc,width,height,n:integer;
  235. begin
  236.   yInc:=GetNumber('Y Increment:',10);
  237.   GetPicSize(width,height);
  238.   y:=0;
  239.   n:=0;
  240.   SetExport('Plot Values');
  241.   repeat
  242.     MakeLineRoi(0,y,width-1,y);
  243.     PlotProfile;
  244.     Export('PLOT',n:4);
  245.     n:=n+1;
  246.     y:=y+yInc;
  247.   until y>=height;
  248. end;
  249.  
  250.  
  251. procedure PlotMeans;
  252. {Plots the mean column in the Results table.}
  253. var
  254.    xmin,xmax,ymin,ymax,i,xscale,yscale:real;
  255.   width,height,margin,pwidth,pheight:integer;
  256.   y,pbottom:integer;
  257. begin
  258.   margin:=40;
  259.   width:=500;
  260.   height:=300;
  261.   ymax:=-999999;
  262.   ymin:=999999;
  263.   for i:=1 to rCount do begin
  264.     y:=rMean[i];
  265.     if y>ymax then ymax:=y;
  266.     if y<ymin then ymin:=y;
  267.   end;
  268.   xmin:=0;
  269.   xmax:=rCount-1;
  270.   SetNewSize(width,height);
  271.   SetForeground(255);
  272.   SetBackground(0);
  273.   MakeNewWindow('Z-Axis Profile Plot');
  274.   pwidth:=width-2*margin;
  275.   pheight:=height-2*margin;
  276.   pbottom:=margin+pheight;
  277.   xscale:=pwidth/(xmax-xmin);
  278.   yscale:=pheight/(ymax-ymin);
  279.   SetForeground(255);
  280.   SetBackground(0); 
  281.   SetLineWidth(1);
  282.   MoveTo(margin,pbottom-(rMean[1]-ymin)*yscale);
  283.   for i:=2 to rCount do
  284.      LineTo(margin+(i-1)*xscale,pbottom-(rMean[i]-ymin)*yscale);
  285.   MoveTo(margin,pbottom);
  286.   LineTo(margin+pwidth,pbottom);
  287.   MoveTo(margin,margin);
  288.   LineTo(margin,margin+pheight);
  289.   SetFont('Geneva');
  290.   SetFontSize(9);
  291.   SetText('Centered');
  292.   MoveTo(margin+4,margin+pheight+12);
  293.   writeln(xmin:1:2);
  294.   MoveTo(margin+pwidth,margin+pheight+12);
  295.   writeln(xmax:1:2);
  296.   SetText('Right Justified');
  297.   MoveTo(margin-2,margin+pheight-5);
  298.   writeln(ymin:1:2);
  299.   MoveTo(margin-2,margin);
  300.   writeln(ymax:1:2);
  301. end;
  302.  
  303. macro 'Plot Z-Axis Profile [Z]';
  304. {Plots the average density of an roi through a stack.}
  305. var
  306.   left,top,width,height,i:integer;
  307. begin
  308.   if (nPics=0) or (nSlices=0) then begin
  309.      PutMessage('This macro requires a stack.');
  310.      exit;
  311.   end;
  312.   GetRoi(left,top,width,height);
  313.   if width=0 then begin
  314.      PutMessage('Selection required.');
  315.      exit;
  316.   end;
  317.   ResetCounter;
  318.   {SetOptions('Mean');}
  319.   for i:= 1 to nSlices do begin
  320.      SelectSlice(i);
  321.      Measure;
  322.   end;
  323.   PlotMeans;
  324.  end;
  325.  
  326.  
  327. macro 'Plot XYZ';
  328. {
  329. Plots X-Y coordinate points with an optional intensity(Z). Values are read from
  330. a 2 or 3 column tab-delimited text file. Data must be scaled as follows:
  331. 0<=X<width; 0<=Y<height; 0<=Z<=255.
  332. }
  333. var
  334.   width,height:integer;
  335. begin
  336.   SaveState;
  337.   width:=500;
  338.   height:=500;
  339.   SetNewSize(width,height);
  340.   SetForeground(255);
  341.   SetBackground(0);
  342.   MakeNewWindow('Plot');
  343.   PlotXYZ;
  344.   RestoreState;
  345. end;
  346.  
  347.